package com.ricervcvcer.brand.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ricervcvcer.brand.entity.TbBrand;
import com.ricervcvcer.brand.entity.TbCategoryBrand;
import com.ricervcvcer.brand.service.ITbBrandService;
import com.ricervcvcer.brand.service.ITbCategoryBrandService;
import com.ricervcvcer.brand.service.ITbCategoryService;
import com.ricervcvcer.brand.vo.BrandCategoryVo;
import com.ricervcvcer.common.util.ResultEntity;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 品牌表，一个品牌下有多个商品（spu），一对多关系 前端控制器
 * </p>
 *
 * @author moge
 * @since 2022-01-05
 */

@Slf4j
@RestController
@RequestMapping("brand")
public class TbBrandController {
    @Autowired
    private ITbBrandService brandService;

    @Autowired
    private ITbCategoryService categoryService;

    @Autowired
    private ITbCategoryBrandService categoryBrandService;

    @ApiOperation("根据主键查询品牌信息")
    @GetMapping("id/{id}")
    public ResponseEntity<TbBrand> getBrandById(
            @ApiParam("品牌主键") @PathVariable("id") Long id){
        TbBrand brand = brandService.getById(id);
        BrandCategoryVo bc =new BrandCategoryVo();
        BeanUtils.copyProperties(brand,bc);

        // 1，获取已经添加的分类集合
        QueryWrapper qw=new QueryWrapper();
        qw.eq("brand_id",brand.getId());
        List<TbCategoryBrand> cbs = categoryBrandService.list(qw);

        // 查询分类
        final List<Long> collect = cbs.stream().map(item -> {
            return  item.getCategoryId();
        }).collect(Collectors.toList());

        // 分装所有已经添加的分类
        bc.setCategories(collect);

        log.debug("根据主键{}查询品牌信息", id);
        return brand != null ? ResponseEntity.ok(bc) : new ResponseEntity<>(HttpStatus.CREATED);
    }

    @ApiOperation("根据主键集合查询品牌信息集合")
    @PostMapping("ids")
    public ResponseEntity<List<TbBrand>> getBrandsByIds(
            @ApiParam("品牌主键集合") @RequestBody List<Long> ids
    ){
        List<TbBrand> brands = brandService.listByIds(ids);
        log.debug("根据主键集合{}查询品牌信息集合", ids);
        return brands != null && brands.size() > 0 ? ResponseEntity.ok(brands) : new ResponseEntity(HttpStatus.CREATED);
    }

    @PostMapping("page")
    @ApiOperation("对品牌进行分页查询")
    public ResultEntity page(
            @ApiParam("页码") @RequestParam Integer page,
            @ApiParam("每页条数") @RequestParam Integer rows,
            @ApiParam("品牌名") @RequestParam String brandName){
        Page<TbBrand> pages = brandService.getBrand(brandName,page,rows);
        return ResultEntity.successWithData(pages);
    }

    @ApiOperation("添加品牌对象")
    @PutMapping("addBrand")
    public ResultEntity add(@ApiParam("品牌对象") @RequestBody BrandCategoryVo bc){
        brandService.add(bc);
        return ResultEntity.successWithoutData();
    }
    @ApiOperation("修改品牌对象")
    @PostMapping("update")
    public ResultEntity update(@ApiParam("品牌对象") @RequestBody BrandCategoryVo bc){
        brandService.updateBrand(bc);
        return ResultEntity.successWithoutData();
    }
}
